#include "osapi.h"
#include "config.h"

//extern dev_status_t localDevStatus;
extern void ICACHE_FLASH_ATTR breathingLightStop(void);

system_info_t systemInfo = {
	.initFlag = 0,
	.startContinue = 0,
	.startCount = 0,
	.devStatus = {
		//       .Switch = 0,
		//      .C_Temperature = 0,
		//      .mode = 0,
		.bright = 100,
		.colorvalue = 0,
		.r = 0,
		.g = 0,
		.b = 0,
		.w = 0,
	},
	.ISCONFIG = 0,
};

LOCAL os_timer_t testTimer;
LOCAL os_timer_t clearStartContinueTimer;

void ICACHE_FLASH_ATTR configReadParam(void)
{
	//uint8_t i = 0;
	system_info_t info;

	//os_printf("configReadParam \n");

	system_param_load(CONFIG_SECTOR, 0, (void *)&info, sizeof(system_info_t));

	//os_printf("systemInfo.initFlag %d \n", info.initFlag);

	if(info.initFlag)
	{
		os_memcpy((uint8_t *)&systemInfo, (uint8_t *)&info, sizeof(system_info_t));
		//      os_memcpy((uint8_t *)&localDevStatus, (uint8_t *)&systemInfo.devStatus, sizeof(dev_status_t));
	}
	else
	{
		//      os_memcpy((uint8_t *)&localDevStatus, (uint8_t *)&systemInfo.devStatus, sizeof(dev_status_t));

		//os_printf("reset param: ");
		//for(i=0; i<sizeof(system_info_t); i++)
		//{
		//    os_printf("0x%02x ", *((uint8_t *)&systemInfo+i));
		//}
		//os_printf("\n");

		systemInfo.initFlag = 1;
		systemInfo.startCount += 1;
		systemInfo.startContinue += 1;
		configWriteParam();
	}
}

void ICACHE_FLASH_ATTR configWriteParam(void)
{
	//uint8_t i = 0;

	//  os_memcpy((uint8_t *)&systemInfo.devStatus, (uint8_t *)&localDevStatus, sizeof(dev_status_t));

	//os_printf("configWriteParam: ");
	//for(i=0; i<sizeof(system_info_t); i++)
	//{
	//    os_printf("0x%02x ", *((uint8_t *)&systemInfo+i));
	//}
	//os_printf("\n");

	system_param_save_with_protect(CONFIG_SECTOR, (void *)&systemInfo, sizeof(system_info_t));
}

void ICACHE_FLASH_ATTR configCheckStart(void)
{
	uint8_t i = 0;
	os_printf("CURRENT MODE========%d\r\n", systemInfo.startContinue);

	if ((systemInfo.startContinue >= 3))
	{
		if (systemInfo.ISCONFIG == 0)
		{
			if (systemInfo.startConfigMode == 0x1)
			{
				//os_printf("enter ap]]]]]]]]]]]]]]]]]]]]\r\n");
				systemInfo.startConfigMode = 0x0;
				//gizwitsSetMode(WIFI_SOFTAP_MODE);
			}
			else if (systemInfo.startConfigMode == 0x0)
			{
				//os_printf("enter airlink]]]]]]]]]]]]]]]]]]]]]]]\r\n");
				systemInfo.startConfigMode = 0x1;
				//gizwitsSetMode(WIFI_AIRLINK_MODE);
			}
		}
		else if (systemInfo.ISCONFIG != 0x0)
		{
			os_printf("enter airlink]]]]]]]]]]]]]]]]]]]]]]]\r\n");
			systemInfo.startConfigMode = 0x0; // go to airlink
			//gizwitsSetMode(WIFI_AIRLINK_MODE);
			systemInfo.ISCONFIG = 0x0;
         		gizwitsSetMode(WIFI_RESET_MODE);       	   
		}
	        systemInfo.startContinue = 0;
	        configWriteParam();
        }


	if (systemInfo.ISCONFIG == 0)
	{
		  if (systemInfo.startConfigMode == 0)
                  {
                                os_printf("enter airlink ]]]]]]]]]]]]]]]]]]]]\r\n");
                                gizwitsSetMode(WIFI_AIRLINK_MODE);
                  }
                  else if (systemInfo.startConfigMode == 0x1)
                  {
                                os_printf("enter ap mode]]]]]]]]]]]]]]]]]]]]]]]\r\n");
                                gizwitsSetMode(WIFI_SOFTAP_MODE);
                  }
	}

// go to change 
#if 0
if (systemInfo.startConfigMode != 0x1 && systemInfo.startConfigMode!=0x2)
{
	os_printf("default setting]]]]]]]]]]]]]]]\r\n");
	systemInfo.startConfigMode = 0x1;
	gizwitsSetMode(WIFI_SOFTAP_MODE);
}
#endif
}

void ICACHE_FLASH_ATTR configSetStartContinue(void)
{
	systemInfo.startContinue += 1;
	configWriteParam();
	os_printf("systemInfo.startContinue %d \n", systemInfo.startContinue);
}

void ICACHE_FLASH_ATTR configClearStartContinue(void)
{
	systemInfo.startContinue = 0;
	configWriteParam();

	//breathingLightStop();
	os_printf("reset start continue!!!!!!!!!!\n");
}

void ICACHE_FLASH_ATTR startClearStartContinueTimer(void)
{
	os_timer_disarm(&clearStartContinueTimer);
	os_timer_setfn(&clearStartContinueTimer, (os_timer_func_t *)configClearStartContinue, NULL);
	os_timer_arm(&clearStartContinueTimer, 10000, 0);
}

void ICACHE_FLASH_ATTR test(void)
{
	configCheckStart();
	os_printf("reset start continue!!!!!!!!!!\n");
}

